<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          class="handle-del mr10"
          @click="setGroup"
          size="small"
          >新增角色</el-button
        >
        <el-input
          v-model="query.roleNm"
          placeholder="角色名"
          class="handle-input mr10"
        ></el-input>
        <el-button type="primary" @click="handleSearch" size="small"
          >搜索</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" align="center">
          <template slot-scope="scope">{{
            (query.pageIndex - 1) * query.pageSize + (scope.$index + 1)
          }}</template>
        </el-table-column>
        <el-table-column
          prop="role_id"
          label="角色编号"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="role_name"
          label="角色名"
          align="center"
        ></el-table-column>
        <!-- <el-table-column prop="details" label="备注" align="center"></el-table-column> -->
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <!-- <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
              >编辑权限</el-button
            > -->
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 新增分组弹出框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="addVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form ref="form" border :model="addForm" label-width="70px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="角色名称">
              <el-input
                placeholder="请输入名称"
                v-model="addForm.roleName"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="角色描述">
              <el-input type="textarea" :rows="2" placeholder="请输入描述" v-model="addForm.roleDesc"></el-input>
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="访问路径">
              <el-input
                type="textarea"
                :rows="2"
                placeholder="请输入路径"
                v-model="addForm.roleUrl"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUpdRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
let addFormParam = { roleName: "", roleDesc: "", roleUrl: "", parentId: "0" };
export default {
  data() {
    return {
      query: {
        roleNm: "",
        pageIndex: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      dialogTitle: "",
      addVisible: false,
      pageTotal: 0,
      addForm: { ...addFormParam },
      idx: -1,
      id: -1,
      checkAll: false,
      menus: [],
      checkedMenus: [],
      isIndeterminate: false,
      isAdd: false
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      this.$axios
        .post(this.$api.role.findAll, {})
        .then(res => {
          console.log("所有的角色信息：" + JSON.stringify(res));
          if (res.data.code == "1" && res.data.success == true) {
            this.tableData = res.data.data;
            this.pageTotal = res.data.data.length;
          }
        })
        .catch(err => {
          console.log(err);
          this.tableData = [];
          this.pageTotal = 0;
        });
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          this.$axios
            .get(this.$api.role.delRoleById + "?id=" + row.role_id)
            .then(res => {
              if (res.data.code == "1" && res.data.success == true) {
                this.$message.success("删除成功");
                this.getData();
              }
              this.addVisible = false;
            })
            .catch(err => {
              console.log(err);
            });
        })
        .then(() => {
          this.$message.success("删除成功");
          this.tableData.splice(index, 1);
        })
        .catch(() => {});
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //打开新增角色的弹框
    setGroup() {
      this.isAdd = true;
      this.addForm = { ...addFormParam };
      this.dialogTitle = "新增角色";
      this.addVisible = true;
    },
    /* // 编辑操作
    handleEdit(index, row) {
      this.idx = index;
      this.isAdd = false;
      this.addForm = { ...addFormParam };
      this.addForm.roleName = row.role_name;
      this.dialogTitle = "编辑权限";
      this.addVisible = true;
    }, */
    //保存或修改角色信息
    saveUpdRole() {
      if (this.isAdd) {
        //console.log("新增角色数据__"+JSON.stringify(this.addForm));
        this.$axios
          .post(this.$api.role.addRole, this.addForm)
          .then(res => {
            console.log("新增角色结果__" + JSON.stringify(res));
            if (res.data.code == "1" && res.data.success == true) {
              this.$message.success("新增成功");
              this.getData();
            }
            this.addVisible = false;
          })
          .catch(err => {
            console.log(err);
          });
      }
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getData();
    },
    handleCheckAllChange(val) {
      console.log(val);
    },
    handleCheckedMenusChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.menus.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.menus.length;
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
